//
//  OSTabBarController.m
//  MiAiApp
//
//  Created by 徐阳 on 2017/5/18.
//  Copyright © 2017年 徐阳. All rights reserved.
//

#import "OSTabBarController.h"
#import "OSRootNavigationController.h"
#import "UITabBar+CustomBadge.h"
#import "OSTabBar.h"
#import "OSHomeVC.h"
#import "OSMineVC.h"
#import "OSApplyHomeVC.h"
@interface OSTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic,strong) NSMutableArray * VCS;//tabbar root VC

@end

@implementation OSTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    //初始化tabbar
    [self setUpTabBar];
    //添加子控制器
    [self setUpAllChildViewController];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


#pragma mark ————— 初始化TabBar —————
-(void)setUpTabBar{
    //设置背景色 去掉分割线
    [self setValue:[OSTabBar new] forKey:@"tabBar"];
    [self.tabBar setBackgroundColor:[UIColor whiteColor]];
    [self.tabBar setBackgroundImage:[UIImage new]];
    //通过这两个参数来调整badge位置
    //    [self.tabBar setTabIconWidth:29];
    //    [self.tabBar setBadgeTop:9];
}

#pragma mark - ——————— 初始化VC ————————
-(void)setUpAllChildViewController{
    
    NSString *key = [NSString stringWithFormat:@"curOrder_%@",kUserInfo.user_id];
    NSDictionary *applyInfo = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSString *orderId = kSTRING(applyInfo[@"id"]);
    _VCS = @[].mutableCopy;
    if (orderId.length > 0) {
        OSApplyHomeVC *fucVc = [[OSApplyHomeVC alloc] init];
        [self setupChildViewController:fucVc title:@"Home" imageName:@"tabbar_home_nor" seleceImageName:@"tabbar_home_pre"];
    }else{
        OSHomeVC *fucVc = [[OSHomeVC alloc] init];
        [self setupChildViewController:fucVc title:@"Home" imageName:@"tabbar_home_nor" seleceImageName:@"tabbar_home_pre"];
    }
    OSMineVC *mineVc = [OSMineVC new];
    [self setupChildViewController:mineVc title:@"Mine" imageName:@"tabbar_mine_nor" seleceImageName:@"tabbar_mine_pre"];
    
    self.viewControllers = _VCS;
}

-(void)setupChildViewController:(UIViewController*)controller title:(NSString *)title imageName:(NSString *)imageName seleceImageName:(NSString *)selectImageName{
    controller.title = title;
    controller.tabBarItem.title = title;//跟上面一样效果
    controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  
    UIColor *selectColor = kColor_T;
    if (@available(iOS 13.0, *)){
        UITabBar *tabBar = [UITabBar appearance];
        [tabBar setTintColor:selectColor];
        [tabBar setUnselectedItemTintColor:[UIColor colorWithHexString:@"#999999"]];
    }else{
        //未选中字体颜色
        [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"],NSFontAttributeName:kFont(14)} forState:UIControlStateNormal];
        //选中字体颜色
        [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:selectColor,NSFontAttributeName:kFont(14)} forState:UIControlStateSelected];
    }

    //包装导航控制器
    OSRootNavigationController *nav = [[OSRootNavigationController alloc] initWithRootViewController:controller];
    
    [_VCS addObject:nav];
}


-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
}

-(void)setRedDotWithIndex:(NSInteger)index isShow:(BOOL)isShow{
    if (isShow) {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleRedDot value:0 atIndex:index];
    }else{
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNone value:0 atIndex:index];
    }
    
}

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
